package com.example.mymusicplayer;

import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.snackbar.Snackbar;

import java.io.File;
import java.util.ArrayList;

public class MusicAdapter  extends RecyclerView.Adapter<MusicAdapter.MyViewHolder> {

    private Context mContext;
    private ArrayList<MusicFiles> mFiles;

    public MusicAdapter(Context context, ArrayList<MusicFiles> mFiles) {
        mContext = context;
        this.mFiles = mFiles;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.music_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.TheMusicName.setText(mFiles.get(position).getTitle());
        byte[] image = getAlbumAct(mFiles.get(position).getPath());
        if (image != null) {
            Glide.with(mContext).asBitmap().load(image).into(holder.TheMusicImg);
        } else {
            Glide.with(mContext).load(R.drawable.ic_baseline_hourglass_empty_24).into(holder.TheMusicImg);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, Media_page.class);
                intent.putExtra("position", position);
                mContext.startActivity(intent);

            }
        });
        holder.MyList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("The Id is " + mFiles.get(position).getId());
                PopupMenu popupMenu = new PopupMenu(mContext, view);
                popupMenu.getMenuInflater().inflate(R.menu.pop, popupMenu.getMenu());
                popupMenu.show();
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        System.out.println("The Id is " + mFiles.get(position).getId());
                        switch (menuItem.getItemId()) {
                            case R.id.delete:
                                Toast.makeText(mContext, "Delete Clicked!", Toast
                                        .LENGTH_SHORT).show();
                                deleteMusic(position, view);
                                break;
                        }
                        return false;
                    }
                });
            }
        });
    }

    private void deleteMusic(int position, View viewItem) {
        mFiles.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mFiles.size());
        Snackbar.make(viewItem, "Music Deleted", Snackbar.LENGTH_LONG).show();
    }

    @Override
    public int getItemCount() {
        return mFiles.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView TheMusicName ;
        private ImageView TheMusicImg,MyList;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            TheMusicImg=itemView.findViewById(R.id.TheMusicAlbum);
            TheMusicName=itemView.findViewById(R.id.MusicName);
            MyList = itemView.findViewById(R.id.MyList);
        }
    }
    private byte[] getAlbumAct(String uri){
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try{
        retriever.setDataSource(uri);
        }
        catch(java.lang.RuntimeException e){
            return null;
        }
        byte[] art = retriever.getEmbeddedPicture();
        retriever.release();
        return art;
    }
}
